﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.IO;
using Redboil.Bss.Model;
using System.Text;

namespace Redboil.Bss.Client {
    public partial class SampleView : UserControl {
        private WebClient webClient;

        public SampleView() {
            InitializeComponent();

            webClient = new WebClient();
        } 

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {

        }

        private void buttonCreate_Click(object sender, RoutedEventArgs e) {
            ConvertHelper

            User user = new User();
            user.Account = "Gene";
            user.IsAdministrator = true;
            user.Password = "123456";
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(User));
            Stream memoryStream = new MemoryStream();
            //StreamWriter streamWriter = new StreamWriter(memoryStream);
            //streamWriter.Write(ea.Result);
            jsonSerializer.WriteObject(memoryStream, user);
            //streamWriter.Close();

            //webClient
            byte[] buf = UnicodeEncoding.UTF8.GetBytes("xx");
            HttpWebRequest webRequest = WebRequest.Create(new Uri("http://localhost:1111/sampleservice")) as HttpWebRequest;
            webRequest.Method = "POST";
            webRequest.ContentLength = buf.Length;
            webRequest.ContentType = "text/html";
            using(Stream postStream = webRequest.GetRequestStream()) {
                //postStream.Write(buf, );
            }
        }

        private void buttonRead_Click(object sender, RoutedEventArgs e) {
            webClient.DownloadStringCompleted += (o, ea) => {
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(User));
                Stream memoryStream = new MemoryStream();
                StreamWriter streamWriter = new StreamWriter(memoryStream);
                streamWriter.Write(ea.Result);
                User user = jsonSerializer.ReadObject(memoryStream) as User;
                streamWriter.Close();
                //MessageBox.Show(user.ToString());

                //this.dataGridUser.ItemsSource = user;

                MessageBox.Show(ea.Result);
                //                JavaScriptSerializer jserializer = new JavaScriptSerializer();
                //2 
                //3 User user = jserializer.Deserialize<User>("{\"UserName\":\"user\",\"TrueName\":\"wange\",\"Address\":{\"City\":\"beijing\",\"Country\":\"China\"},\"AddressList\":[{\"City\":\"ShangHai\",\"Country\":\"China\"},{\"City\":\"ShiJiaZhuang\",\"Country\":\"China\"}]}");//将Json字符串转换为实体类

                //            using(Stream responseStream = ea.Result 
                //                response.GetResponseStream())
                //{
                //}


                //this.dataGridUser.ItemsSource = ea.Result;
            };

            webClient.DownloadStringAsync(new Uri("http://localhost:1111/sampleservice"));
        }

        private void buttonUpdate_Click(object sender, RoutedEventArgs e) {

        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e) {

        }
    }
}
